import { Component, OnInit } from '@angular/core';
import { AlertController } from '@ionic/angular';
import { Router, NavigationExtras,ActivatedRoute } from '@angular/router';
import { HttpClient, HttpHeaders } from "@angular/common/http";
import { AuthTokenService } from '@services/auth-token.service';
import {CustomToastProviderService} from '@common-components/common-toast/custom-toast-provider.service';
import { EncryptService } from '@services/encrypt.service';
import { ModalController } from '@ionic/angular';
import { CommonNoPaymentPwdPage } from '@common-components/common-no-payment-pwd/common-no-payment-pwd.page';
@Component({
  selector: 'app-account-detail',
  templateUrl: './account-detail.page.html',
  styleUrls: ['./account-detail.page.scss'],
})
export class AccountDetailPage implements OnInit {
  optionEnable:boolean = false;

  detail : any;
  constructor(
    private alertController:AlertController,
    private router:Router,
    private authTokenService: AuthTokenService,
    public http: HttpClient,
    public modalController: ModalController,
    private encryptService:EncryptService,
    private customToast:CustomToastProviderService,
    private route : ActivatedRoute
  ) {
    this.route.queryParams.subscribe((data)=>{
      this.detail=data;
    });

   }

  ngOnInit() {
  }

  changeEnable(evt){
    this.optionEnable = !this.optionEnable;
  }

  toRemove(){
    this.alertController.create({
      header: 'Are you sure you want to unbind this account ?',
      buttons: [
        {
          text: 'Cancel',
          role: 'cancel',
          cssClass: 'secondary',
          handler: () => {
            

          }
        }, {
          text: 'Confirm',
          handler: () => {

            // this.confirmRemoveAccount();
            this.checkPaymentPwd();
            
          }
        }
      ]
    }).then((resolve)=>{
      resolve.present();
    });
   
  }

  confirmRemoveAccount(){
    this.alertController.create({
      header: 'Please enter payment password',
      inputs: [
        {
          name: 'pwd',
          type: 'password',
          placeholder: 'Password',
          checked:true,
          id:"modalPwd"
        }
      ],
      buttons: [
        {
          text: 'Cancel',
          role: 'cancel',
          cssClass: 'secondary',
          handler: () => {
           
          }
        }, {
          text: 'Confirm',
          handler: (data) => {
            let pwd = data.pwd;
            // paymentPassword
            this.removeCard(pwd);
          }
        }
      ]
    }).then(
      (resolve)=>{
        resolve.present();
        let modalPwd = document.getElementById('modalPwd');
        modalPwd.setAttribute('maxlength','6');
      }
    );
  }

  appuserid = this.authTokenService.getAppUserId();

  checkPaymentPwd(){
    let url = '/scm/app/checkPaymentPwdExistence.do';
 
    let requestData = {
      appUserId: this.appuserid,
    };
    this.http.post(url, requestData).subscribe((response:any) => {
      let body = response.responseBody;
      if (body.firstPwd) {
        this.modalController.create({
          component: CommonNoPaymentPwdPage,
          cssClass: 'no_pwd_custom_modal',
          componentProps: {
            "url":location.pathname
          }
        }).then(resolve=>{resolve.present()});
      }else{
        this.confirmRemoveAccount();
      }
    });
  }

  removeCard(pwd){
    let url = '/scm/app/deleteBankAccount.do';  
    pwd = this.encryptService.md5Ecrypt(pwd);
    let request = {
      "appuserid":this.appuserid,
      "paymentPassword":pwd,
      "id":this.detail.id
    };    
    this.http.post(url,request).subscribe(
      (response:any)=>{
        let body = response.responseBody;
        
        if(body == 0){
          this.customToast.show({
            icon:false,
            iconSuccess:false,
            message:'Payment password error',
            duration:2000
          });
          // return;
        }else{
          let navigationExtras: NavigationExtras = {
            queryParams: {
              removeSuccess:true
            }
          };
          this.router.navigate(['/tabs/myAccount'], navigationExtras);
        }
      });
  }
}
